package ru.spb.osll.myfit.wikiverifier;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ru.spb.osll.myfit.common.Table;
import ru.spb.osll.myfit.common.TableStyle;
import ru.spb.osll.myfit.wikiverifier.result.TableVerificationResult;
import ru.spb.osll.myfit.wikiverifier.suggester.DoFixtureSuggester;
import ru.spb.osll.myfit.wikiverifier.suggester.SetUpFixtureSuggester;
import ru.spb.osll.myfit.wikiverifier.suggester.TemplateSuggester;

public class SuggestionProvider {
	private List<Table> m_templates;
	private Map<TableStyle, TemplateSuggester> m_suggesters;

	public SuggestionProvider(List<Table> templates) {
		m_templates = templates;
		m_suggesters = new HashMap<TableStyle, TemplateSuggester>();
		m_suggesters.put(TableStyle.DO_FIXTURE, new DoFixtureSuggester());
		m_suggesters.put(TableStyle.ONE_LINE_FIXTIRE, new DoFixtureSuggester());
		m_suggesters.put(TableStyle.SETUP_FIXTURE, new SetUpFixtureSuggester());
	}

	public void provideSuggestion(TableVerificationResult res) {
		res.setSuggestion(getSuggestion(res.getTable()));
	}

	private Table getSuggestion(Table table) {
		TemplateSuggester selectedSuggester = m_suggesters.get(0);
		Table suggestedTemplate = m_templates.get(0);
		double minDistance = Double.MAX_VALUE;

		for (Table template : m_templates) {

			TemplateSuggester suggester = m_suggesters.get(template.getStyle());
			double distance = suggester.distance(table, template);

			if (distance < minDistance) {
				selectedSuggester = suggester;
				suggestedTemplate = template;
				minDistance = distance;
			}
		}

		return selectedSuggester.getSuggestion(table, suggestedTemplate);
	}

}
